<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */

$isSpecial = (int)$_data["isSpecial"];
if(strlen($_data["isSpecial"]) == 0)$isSpecial = 1;
$intSpecialNumType = (int)$_data["SpecialNumType"];
$isRealShow = (int)$_data["isRealShow"];
$intGID = (int)$_data["GambleID"];
$strBT = trim($_data["Bet_Type"]);
$arrOtherMsg = explode(",", trim($_data["OtherMsg"]));
$strLottoNumber = trim($_data["strLottoNumber"]);

if($_data["isGetSuppleOdds"] == "Y"){
	$ch = curl_init ();

	curl_setopt($ch,CURLOPT_URL, "http://".$_KSysParam[SuppleIP]."/k_admin/Supplement3D/index.php?op=sendOdds&LottoID=".$intLottoID."&Bet_Type=".$strBT."&isSpecial=".$isSpecial."&GameType=".$_data["GameType"]);
	//curl_setopt ($ch, CURLOPT_VERBOSE, 1);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
	curl_setopt ($ch, CURLOPT_HEADER, 0);
	curl_setopt ($ch, CURLOPT_HTTPHEADER, array(
			"Accept:image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-shockwave-flash, */*",
			"User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)",
			"Host:".$_KSysParam[SuppleUrl],
			"Connection:Keep-Alive"));
	$strCont = curl_exec ($ch);
	curl_close ($ch);
	$strCont = preg_replace("/<[^\$]+>/", "", $strCont);

	if(preg_match("/arrGame/", $strCont)){
		eval($strCont);
	}else{
		$arrGame = array();
	}
}

$arrSumStake = array();
$arrSumPayOff = array();
$arrSumCommission = array();
$arrSumSuppleStake = array();
$arrSumSupplePayOff = array();
$arrSumSuppleCommission = array();
$arrBT = explode(",", $strBT);
$strSQL = "select * from `bet_TodayGame".$intLottoID."` where LottoID = ".$intLottoID." and GambleID = '".$intGID."' and Bet_Type in (".$strBT.") ";
switch($strBT){
	case "92":
	case "93":
		$intBT = (int)$strBT;
		$strRealTip = ($isRealShow == 0)?"":"Real";

		//找出三定位 1000 個號碼的總注單量
		$strSumSQL = "select Sum(tolWager) as tolWager, Sum(tol".$strRealTip."Stake) as tolStake, Sum(tol".$strRealTip."PayOff) as tolPayOff, Sum(tolCommission) as tolCommission, Sum(tolSuppleWager) as tolSuppleWager, Sum(tolSuppleStake) as tolSuppleStake, Sum(tolSupplePayOff) as tolSupplePayOff, Sum(tolSuppleCommission) as tolSuppleCommission from `bet_TodayGame".$intLottoID."` where LottoID = ".$intLottoID." and GambleID = '".$intGID."' and Bet_Type = ".$intBT;
		if(!$RS = PMA_mysql_query($strSumSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find TodayGame Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
			ErrMsgOutputHTML("Error", "", 1);
		}
		$Rows = PMA_mysql_fetch_object($RS);

		$arrSumStake[$intBT] = $Rows -> tolStake;
		$arrSumPayOff[$intBT] = $Rows -> tolPayOff;
		$arrSumCommission[$intBT] = $Rows -> tolCommission;
		$arrSumSuppleStake[$intBT] = $Rows -> tolSuppleStake;
		$arrSumSupplePayOff[$intBT] = $Rows -> tolSupplePayOff;
		$arrSumSuppleCommission[$intBT] = $Rows -> tolSuppleCommission;

		switch($arrOtherMsg[0]){
			case "Sort": //排序
				$strSQL = "select *, (".($Rows -> tolStake - $Rows -> tolCommission - $Rows -> tolSuppleStake + $Rows -> tolSuppleCommission)." - tol".$strRealTip."Stake - tol".$strRealTip."PayOff + tolSuppleStake + tolSupplePayOff) as preWin from `bet_TodayGame".$intLottoID."` where LottoID = ".$intLottoID." and GambleID = '".$intGID."' and Bet_Type = ".$intBT." order by preWin, Number limit ";
				$strSQL .= ($strBT != "93")?"100":"220";
				break;

			default:
				$strSQL .= "and Number in (".$strLottoNumber.") order by Number";
				break;
		}
		break;

	default:
		$strSQL .= " order by Bet_Type";
		break;
}
//print $strSQL;
//print_r($arrGame);

if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Find TodayGame Data Error!!";
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

if(PMA_mysql_num_rows($RS) == 0){
	$GLOBALS["errormessage"] = "No TodayGame Data!!";
	//$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

$intNum = -1;
while($Rows = PMA_mysql_fetch_object($RS)){
	if($isRealShow == 0){
		$tolStake = $Rows -> tolStake;
		$tolPayOff = $Rows -> tolPayOff;
		$tolWager = $Rows -> tolWager;
		$tolCommission = 0;
	}else{
		$tolStake = $Rows -> tolRealStake;
		$tolPayOff = $Rows -> tolRealPayOff;
		$tolWager = $Rows -> tolWager;
		$tolCommission = $Rows -> tolCommission;
	}

	if(empty($tolStake))$tolStake = 0;
	if(empty($tolPayOff))$tolPayOff = 0;
	if(empty($tolCommission))$tolCommission = 0;
	if(empty($tolWager))$tolWager = 0;
	$tolSuppleWager = (empty($Rows -> tolSuppleWager))?0:$Rows -> tolSuppleWager;
	$tolSuppleStake = (empty($Rows -> tolSuppleStake))?0:$Rows -> tolSuppleStake;
	$tolSupplePayOff = (empty($Rows -> tolSupplePayOff))?0:$Rows -> tolSupplePayOff;
	$tolSuppleCommission = (empty($Rows -> tolSuppleCommission))?0:$Rows -> tolSuppleCommission;
	if($_data["isGetSuppleOdds"] == "Y"){
		$intOID = $arrGame[$Rows -> Bet_Type][$Rows -> Number][4];
		$flOdds = $arrGame[$Rows -> Bet_Type][$Rows -> Number][5];
	}else{
		$intApointNum = $_arrGLineNumToApointNum[$Rows -> LottoID][$Rows -> Bet_Type][$Rows -> Number];
		if($intApointNum == null)$intApointNum = 0;
		$flGLine = $_arrGambleType[$Rows -> Bet_Type][$intApointNum];

		$intOID = $Rows -> OddsID;
		$flOdds = $Rows -> Odds + $flGLine;
	}
	$intBT = $Rows -> Bet_Type;
	$intNum = ($Rows -> Bet_Type == 92 ||  $Rows -> Bet_Type == 93)?($intNum+1):$Rows -> Number;

	$strCon .= "parent.Game".$intBT."[".$intNum."]=new Array('".$Rows -> LottoID."','".$Rows -> GambleID."','".$Rows -> Bet_Type."','".$Rows -> Number."','".$intOID."','".$flOdds."','".$Rows -> isStopBet."','".$tolWager."','".$tolStake."','".$tolPayOff."','".$tolCommission."','".$tolSuppleWager."','".$tolSuppleStake."','".$tolSupplePayOff."','".$tolSuppleCommission."');\n";
	switch($Rows -> Bet_Type){
		case 92:
		case 93:
			break;

		default:
			$arrSumStake[$Rows -> Bet_Type] += $tolStake;
			$arrSumPayOff[$Rows -> Bet_Type] += $tolPayOff;
			$arrSumCommission[$Rows -> Bet_Type] += $tolCommission;
			$arrSumSuppleStake[$Rows -> Bet_Type] += $tolSuppleStake;
			$arrSumSupplePayOff[$Rows -> Bet_Type] += $tolSupplePayOff;
			$arrSumSuppleCommission[$Rows -> Bet_Type] += $tolSuppleCommission;
			break;
	}
}
/*=================================================================*/
header("Cache-Control: no-store, no-cache, must-revalidate");	  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");															  // HTTP/1.0
?><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript">
<!--
<?php 
print $strCon."\n";

foreach($arrSumStake as $intBT => $val){
	print "parent.sumStake[".$intBT."] = ".round($arrSumStake[$intBT]).";\n";
	print "parent.sumPayOff[".$intBT."] = ".round($arrSumPayOff[$intBT]).";\n";
	print "parent.sumCommission[".$intBT."] = ".round($arrSumCommission[$intBT]).";\n";
	print "parent.sumSuppleStake[".$intBT."] = ".round($arrSumSuppleStake[$intBT]).";\n";
	print "parent.sumSupplePayOff[".$intBT."] = ".round($arrSumSupplePayOff[$intBT]).";\n";
	print "parent.sumSuppleCommission[".$intBT."] = ".round($arrSumSuppleCommission[$intBT]).";\n";
}
switch($strBT){
	case "51";
	case "52";
	case "53";
	case "54";
	case "55";
	case "56";
	case "57";
	case "58";
	case "59";
	case "60";
	case "61";
	case "62";
	case "63";
	case "84";
	case "85";
	case "86";
	case "51,52,53,54,55,56,57,58,59,60,61,62,63,84,85,86":
		print "parent.ShowNumberOdds51(51);\n";
		break;

	//雙飛
	case "64";
	case "65";
	case "66";
	case "67";
	case "64,65,66";
		print "parent.ShowTwinNumer(".$strBT.");\n";
		break;

	//跨度過關
	case "87";
	case "88";
	case "89";
	case "90";
	case "91";
	case "87,88,89,90,91":
		print "parent.ShowOPNumber(87);\n";
		break;

	//三定位
	case "92";
		print "parent.ShowTrinityNumber(92);\n";
		break;

	//三字
	case "93";
		print "parent.Show3WordNumber(93);\n";
		break;
}

if($_data["isGetSuppleOdds"] == "Y" && $arrResult["WagerCon"] != ""){
	$arrResult["WagerCon"] = str_replace(chr(10), "", $arrResult["WagerTip"]."<br />".$arrResult["WagerCon"]);
	$arrResult["WagerCon"] = str_replace(chr(13), "", $arrResult["WagerCon"]);
	print "parent.SuppleResult.innerHTML='".addslashes($arrResult["WagerCon"])."';\n";
	print "parent.open_SuppleResultWin();\n";
}
?>
//-->
</script>